﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using SqlHelp;
using CommonCs;
using Model;
using System.Data.SqlClient;

namespace DataServer
{
    public class UpFileData
    {
        public DataTable GetAllFile(int size ,int index)
        {
            String sql = "select b.Id,b.FileName,a.Uid as CreateUser,a.id as userid,b.CreateDate,b.FileUrl from Users as a ,FilesTable as b where a.Id = b.CreateUser";

            DataTable dt = new DataTable();
            try
            {
                dt = SQLHELP.getdiyerTable(size, index, sql, "FilesTable");
                if (dt == null || dt.Rows.Count == 0)
                {
                    return null;

                }
                else
                {
                    return dt;
                }
            }
            catch (Exception)
            {
                Common.WriteFile(CommonCs.Exceptions.Abnormal, "获取所有上传文件数据异常[带分页],msg:[" + Exceptions.getallfiles + "]!");
                throw new Exception(Exceptions.getallfiles);
            }
        }

        public DataTable GetAllFile()
        {
            String sql = "select b.Id,b.FileName,a.Uid as CreateUser,a.id as userid,b.CreateDate,b.FileUrl from Users as a ,FilesTable as b where a.Id = b.CreateUser";

            DataTable dt = new DataTable();
            try
            {
                dt = SQLHELP.GetDataTableNoParm(sql);
                if (dt == null || dt.Rows.Count == 0)
                {
                    return null;

                }
                else
                {
                    return dt;
                }
            }
            catch (Exception)
            {
                Common.WriteFile(CommonCs.Exceptions.Abnormal, "获取所有上传文件数据异常[不带分页],msg:[" + Exceptions.getallfiles + "]!");
                throw new Exception(Exceptions.getallfiles);
            }
        }

        public int GetFilesCount(int size,int index)
        {
            DataTable dt = GetAllFile(size,index);
            if (dt == null)
            {
                return 0;
            }
            return dt.Rows.Count;
        }

        public bool AddFile(FilesTable file)
        {
            String sql = "insert into FilesTable values(@FileName,@CreateDate,@CreateUser,@FileUrl)";
            SqlParameter[] parm = new SqlParameter[]{
                new SqlParameter("@FileName",file.FileName),
                new SqlParameter("@CreateDate",file.CreateDate),
                new SqlParameter("@CreateUser",file.CreateUser),
                new SqlParameter("@FileUrl",file.FileUrl)
            };
            int result = 0;
            try
            {
                result = SQLHELP.GetNonQueryWithParm(sql, parm);
            }
            catch (Exception)
            {
                Common.WriteFile(CommonCs.Exceptions.Abnormal, "添加上传文件到数据库异常,msg:[" + Exceptions.uploadfileintodb + "]!");
                throw new Exception(Exceptions.uploadfileintodb);
            }
            if (result == 1)
            {
                return true;
            }
            return false;
        }

        public bool DelFile(int id)
        {
            String sql = "delete FilesTable where id=@id";
            SqlParameter[] parm = new SqlParameter[]{
                new SqlParameter("@id",id)
            };
            int result = 0;
            try
            {
                result = SQLHELP.GetNonQueryWithParm(sql, parm);
            }
            catch (Exception)
            {
                Common.WriteFile(CommonCs.Exceptions.Abnormal, "删除上传文件异常,msg:[" + Exceptions.deletefile + "]!");
                throw new Exception(Exceptions.deletefile);
            }
            if (result == 1)
            {
                return true;
            }
            return false;
        }
    }
}
